home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Point.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  189 lines

  1. /*
  2.  * @(#)Point.java    1.14 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. /**
  17.  * The <code>Point</code> class represents a location in a
  18.  * two-dimensional (<i>x</i>, <i>y</i>) coordinate space.
  19.  *
  20.  * @version     1.14, 07/01/98
  21.  * @author     Sami Shaio
  22.  * @since       JDK1.0
  23.  */
  24. public class Point implements java.io.Serializable {
  25.     /**
  26.      * The <i>x</i> coordinate. 
  27.      * @since   JDK1.0
  28.      */
  29.     public int x;
  30.  
  31.     /**
  32.      * The <i>y</i> coordinate. 
  33.      * @since   JDK1.0
  34.      */
  35.     public int y;
  36.  
  37.     /*
  38.      * JDK 1.1 serialVersionUID 
  39.      */
  40.     private static final long serialVersionUID = -5276940640259749850L;
  41.  
  42.     /**
  43.      * Constructs and initializes a point at the origin 
  44.      * (0, 0) of the coordinate space. 
  45.      * @param       x   the <i>x</i> coordinate.
  46.      * @param       y   the <i>y</i> coordinate.
  47.      * @since       JDK1.1
  48.      */
  49.     public Point() {
  50.     this(0, 0);
  51.     }
  52.  
  53.     /**
  54.      * Constructs and initializes a point with the same location as
  55.      * the specified <code>Point</code> object.
  56.      * @param       p a point.
  57.      * @since       JDK1.1
  58.      */
  59.     public Point(Point p) {
  60.     this(p.x, p.y);
  61.     }
  62.  
  63.     /**
  64.      * Constructs and initializes a point at the specified 
  65.      * (<i>x</i>, <i>y</i>) location in the coordinate space. 
  66.      * @param       x   the <i>x</i> coordinate.
  67.      * @param       y   the <i>y</i> coordinate.
  68.      * @since       JDK1.0
  69.      */
  70.     public Point(int x, int y) {
  71.     this.x = x;
  72.     this.y = y;
  73.     }
  74.  
  75.     /**
  76.      * Returns the location of this point.
  77.      * This method is included for completeness, to parallel the
  78.      * <code>getLocation</code> method of <code>Component</code>.
  79.      * @return      a copy of this point, at the same location.
  80.      * @see         java.awt.Component#getLocation
  81.      * @see         java.awt.Point#setLocation(java.awt.Point)
  82.      * @see         java.awt.Point#setLocation(int, int)
  83.      * @since       JDK1.1
  84.      */
  85.     public Point getLocation() {
  86.     return new Point(x, y);
  87.     }    
  88.  
  89.     /**
  90.      * Sets the location of the point to the specificed location.
  91.      * This method is included for completeness, to parallel the
  92.      * <code>setLocation</code> method of <code>Component</code>.
  93.      * @param       p  a point, the new location for this point.
  94.      * @see         java.awt.Component#setLocation(java.awt.Point)
  95.      * @see         java.awt.Point#getLocation
  96.      * @since       JDK1.1
  97.      */
  98.     public void setLocation(Point p) {
  99.     setLocation(p.x, p.y);
  100.     }    
  101.  
  102.     /**
  103.      * Changes the point to have the specificed location.
  104.      * <p>
  105.      * This method is included for completeness, to parallel the
  106.      * <code>setLocation</code> method of <code>Component</code>.
  107.      * Its behavior is identical with <code>move(int, int)</code>.
  108.      * @param       x  the <i>x</i> coordinate of the new location.
  109.      * @param       y  the <i>y</i> coordinate of the new location.
  110.      * @see         java.awt.Component#setLocation(int, int)
  111.      * @see         java.awt.Point#getLocation
  112.      * @see         java.awt.Point#move(int, int)
  113.      * @since       JDK1.1
  114.      */
  115.     public void setLocation(int x, int y) {
  116.     move(x, y);
  117.     }    
  118.  
  119.     /**
  120.      * Moves this point to the specificed location in the 
  121.      * (<i>x</i>, <i>y</i>) coordinate plane. This method
  122.      * is identical with <code>setLocation(int, int)</code>.
  123.      * @param       x  the <i>x</i> coordinate of the new location.
  124.      * @param       y  the <i>y</i> coordinate of the new location.
  125.      * @see         java.awt.Component#setLocation(int, int)
  126.      * @since       JDK1.0
  127.      */
  128.     public void move(int x, int y) {
  129.     this.x = x;
  130.     this.y = y;
  131.     }    
  132.  
  133.     /**
  134.      * Translates this point, at location (<i>x</i>, <i>y</i>), 
  135.      * by <code>dx</code> along the <i>x</i> axis and <code>dy</code> 
  136.      * along the <i>y</i> axis so that it now represents the point 
  137.      * (<code>x</code> <code>+</code> <code>dx</code>, 
  138.      * <code>y</code> <code>+</code> <code>dy</code>). 
  139.      * @param       dx   the distance to move this point 
  140.      *                            along the <i>x</i> axis.
  141.      * @param       dy    the distance to move this point 
  142.      *                            along the <i>y</i> axis.
  143.      * @since       JDK1.0
  144.      */
  145.     public void translate(int x, int y) {
  146.     this.x += x;
  147.     this.y += y;
  148.     }    
  149.  
  150.     /**
  151.      * Returns the hashcode for this point.
  152.      * @return      a hash code for this point.
  153.      * @since       JDK1.0
  154.      */
  155.     public int hashCode() {
  156.     return x ^ (y*31);
  157.     }
  158.  
  159.     /**
  160.      * Determines whether two points are equal. Two instances of
  161.      * <code>Point</code> are equal if the values of their 
  162.      * <code>x</code> and <code>y</code> member fields, representing
  163.      * their position in the coordinate space, are the same.
  164.      * @param      obj   an object to be compared with this point.
  165.      * @return     <code>true</code> if the object to be compared is
  166.      *                     an instance of <code>Point</code> and has
  167.      *                     the same values; <code>false</code> otherwise.
  168.      * @since      JDK1.0
  169.      */
  170.     public boolean equals(Object obj) {
  171.     if (obj instanceof Point) {
  172.         Point pt = (Point)obj;
  173.         return (x == pt.x) && (y == pt.y);
  174.     }
  175.     return false;
  176.     }
  177.  
  178.     /**
  179.      * Returns a representation of this point and its location
  180.      * in the (<i>x</i>, <i>y</i>) coordinate space as a string.
  181.      * @return    a string representation of this point, 
  182.      *                 including the values of its member fields.
  183.      * @since     JDK1.0
  184.      */
  185.     public String toString() {
  186.     return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  187.     }
  188. }
  189.